home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / FIXCR.CPP < prev    next >
C/C++ Source or Header  |  1994-11-18  |  4KB  |  185 lines

  1. // FIXCR.CPP                                 1          1    6666
  2. // Dave Harris                                11         11   6
  3. // Compiled using Borland C++ ver 3.1       1 1        1 1   6666
  4. // 03-03-94                                  1     ..   1   6   6
  5. //                                           11111 .. 11111  666
  6. ////////////////////////////////////////////////////////////////////////
  7.  
  8. #include "au.hpp"
  9. #include <time.h>
  10.  
  11. #define PROGRAM "FIXCR"   // Name of module
  12. /*********************************************************************/
  13.  
  14. #define BUF_SIZE 256
  15. #define TMP_FILE "FIXCR.TMP"
  16.  
  17. typedef struct
  18. {
  19.     char tmp_file[FLENGTH];
  20. } FIXCR_INFO;
  21.  
  22. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  23. static void buffered_write(HANDLE *handle, char ch, char *buffer, int *pos)
  24. {
  25.     if (ch == -1)    /* Write the string even if not full */
  26.     {
  27.         if (handle->write_raw(buffer, *pos) != *pos)
  28.         {
  29.             fprintf(stderr, "\aWrite Error %d\n", errno);
  30.             exit(1);
  31.         }
  32.         *pos = 0;
  33.     }
  34.     else
  35.     {
  36.         buffer[*pos] = ch;
  37.         (*pos)++;
  38.         if (*pos == BUF_SIZE)
  39.         {
  40.             if (handle->write_raw(buffer, *pos) < 0)
  41.             {
  42.                 fprintf(stderr, "\aWrite Error %d\n", errno);
  43.                 exit(1);
  44.             }
  45.             *pos = 0;
  46.         }
  47.     }
  48. }
  49. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  50. static int fixcr(AU *au, char *file_name)
  51. {
  52.     HANDLE in_handle, out_handle;
  53.     int  ch, ch2;
  54.     int  opt;
  55.     int  modified = FALSE;
  56.     int  binary_ok = FALSE;     /* File is binary, but user answered yes */
  57.     char buffer[BUF_SIZE];
  58.     int  pos = 0;
  59.     FIXCR_INFO *in = (FIXCR_INFO *)au->info;
  60.  
  61.     if (!au->no_extra)
  62.         printf("@?6Processing @?1%s@?H\n", file_name);
  63.  
  64.     if (in_handle.open(au, file_name, O_RDONLY | O_BINARY) != SUCCESS)
  65.         return -1;
  66.  
  67.     if (out_handle.open(au, in->tmp_file, O_RDWR | O_BINARY | O_CREAT) != SUCCESS)
  68.     {
  69.         in_handle.close();
  70.         return -1;
  71.     }
  72.  
  73.     ch = in_handle.read_char();
  74.     for (;;)
  75.     {
  76.         if (ch == EOF)
  77.             break;
  78.  
  79.         if (is_binary_file_char(ch) && !binary_ok)
  80.         {
  81.             fprintf(stderr, "File %s appears to be a binary file.  Still fix it Y/N:",
  82.                     file_name);
  83.             opt = toupper(my_getch());
  84.             fprintf(stderr, "\n");
  85.             if (opt != 'Y')
  86.             {
  87.                 modified = FALSE;
  88.                 break;
  89.             }
  90.             else
  91.                 binary_ok = TRUE;
  92.         }
  93.  
  94.         ch2 = in_handle.read_char();
  95.  
  96.         if (ch == '\r' && ch2 != '\n' || ch == '\n')
  97.         {
  98.             buffered_write(&out_handle, '\r', buffer, &pos);
  99.             buffered_write(&out_handle, '\n', buffer, &pos);
  100.             ch = ch2;
  101.             modified = TRUE;
  102.         }
  103.         else if (ch == '\r' && ch2 == '\n')
  104.         {
  105.             buffered_write(&out_handle, '\r', buffer, &pos);
  106.             buffered_write(&out_handle, '\n', buffer, &pos);
  107.             ch = in_handle.read_char();
  108.         }
  109.         else
  110.         {
  111.             buffered_write(&out_handle, ch, buffer, &pos);
  112.             ch = ch2;
  113.         }
  114.     }
  115.     buffered_write(&out_handle, -1, buffer, &pos);
  116.     out_handle.close();
  117.     in_handle.close();
  118.     if (modified)
  119.     {
  120.         unlink(file_name);
  121.         rename(in->tmp_file, file_name);
  122.         au->number_changed++;
  123.     }
  124.     else
  125.         unlink(in->tmp_file);
  126.  
  127.     au->number_processed++;
  128.     return 0;
  129. }
  130. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  131. static BYTE parse_comm_line(AU *au, char option, char *, PARSE_TYPE type)
  132. {
  133.     switch (type)
  134.     {
  135.     case PARSE_PARAM_OPTION:
  136.         switch (option)
  137.         {
  138.         case '?':
  139.             au_standard_opt_header(au, "FIXcr", NULL);
  140.             exit(0);
  141.         default:
  142.             au_invalid_option(au, PROGRAM, option);
  143.         }
  144.         return TRUE;
  145.     }
  146.     return FALSE;
  147. }
  148. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  149. static void end_program(void)
  150. {
  151.     FIXCR_INFO *in = (FIXCR_INFO *)glob_au->info;
  152.  
  153.     unlink(in->tmp_file);
  154.     return;
  155. }
  156.  
  157. /*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
  158. int main_fixcr(AU *au, int argc, char *argv[])
  159. {
  160.     FIXCR_INFO *in;
  161.  
  162.     in = new FIXCR_INFO;
  163.     memset(in, '\0', sizeof(FIXCR_INFO));
  164.     au->info = in;
  165.  
  166.     generic_parse_comm_line(au, argc, argv, parse_comm_line);
  167.  
  168.     build_fname(in->tmp_file, au->cur_directory, TMP_FILE);
  169.     atexit(end_program);   /* Remove temporary if it dies */
  170.  
  171.     process_files(au, fixcr);
  172.  
  173.     if (!au->no_extra)
  174.         au_printf_c(au, 15, "\nFiles Processed = %d\n", au->number_processed);
  175.  
  176.     if (au->number_processed > 0)
  177.     {
  178.         if (!au->no_extra)
  179.             au_printf_c(au, 15, "Files Modified  = %d\n", au->number_changed);
  180.     }
  181.  
  182.     return 0;
  183. }
  184.  
  185.